package cn.chench.exception;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.result.view.ViewResolver;

/**
 * 自定义异常处理配置
 * @author chencaihui
 * @datetime 创建时间：2020年11月11日 下午2:31:41
 */
@Configuration
public class ExceptionConfig {

	/**
	 * 自定义异常处理[@@]注册Bean时依赖的Bean，会从容器中直接获取，所以直接注入即可
	 */
	@Primary
	@Bean
	@Order(Ordered.HIGHEST_PRECEDENCE)
	public ErrorWebExceptionHandler errorWebExceptionHandler(ObjectProvider<List<ViewResolver>> viewResolversProvider,
			ServerCodecConfigurer serverCodecConfigurer) {
		JsonExceptionHandler jsonExceptionHandler = new JsonExceptionHandler();
		jsonExceptionHandler.setViewResolvers(viewResolversProvider.getIfAvailable(Collections::emptyList));
		jsonExceptionHandler.setMessageWriters(serverCodecConfigurer.getWriters());
		jsonExceptionHandler.setMessageReaders(serverCodecConfigurer.getReaders());
		return jsonExceptionHandler;
	}

}
